<?php
class MsgUserTestController extends CpController
{
    public function init(){
        $url = new Url();
        $this->breadcrumbs[] = array('name'=>'QT tin nhắn','link'=>$url->createUrl("msgUser/index"),'class'=>'');
    }
    public function actionIndex(){
        $row_per_page = 10; $arr_game_id = array();  
        $this->breadcrumbs[] = array('name'=>'DS tin nhắn','link'=>'#','class'=>'active');
        $from_date = isset($_GET["from_date"]) ? trim($_GET["from_date"]):"";
        $to_date = isset($_GET["to_date"]) ? trim($_GET["to_date"]):"";
        $keyword = isset($_GET["keyword"]) ? trim($_GET["keyword"]):"";
        $status = isset($_GET["status"]) ? intval($_GET["status"]):2;
        $page = isset($_GET["page"]) ? intval($_GET["page"]):1;
        list($max_page,$count,$data) = AMsgUser::getDataSearch($from_date,$to_date,$keyword,$status,$page,$row_per_page);

        $this->render("index",array("max_page"=>$max_page,"count"=>$count,"data"=>$data,
        "from_date"=>$from_date,"to_date"=>$to_date,"keyword"=>$keyword,"status"=>$status,"page"=>$page));
    }

    public function actionAjaxQuickUpdate(){
        $time_now = time();
        $status = isset($_POST["status"]) ? intval($_POST["status"]):0;
        $title_msg = isset($_POST["title_msg"]) ? Common::cleanQuery($_POST["title_msg"]):"";
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id >0){
            $data_old = AMsgUser::getDataById($id);
            $data = array(
            "status"=>array("value"=>$status,"type"=>1),
            "title"=>array("value"=>$title_msg,"type"=>2),
            "id"=>array("value"=>$id,"type"=>1)
            );
            $table = "c_msg_user";
            $result = CommonDB::updateRow($table,$data,array("id"));
            if($result >0){
                $act = 2;
                $obj = 29;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',title: '.$data_old["title"].',status: '.$data_old["status"].'} thành';
                $content .= ' {id: '.$id.',name: '.$title_msg.',status: '.$status.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionEdit(){
        $this->breadcrumbs[] = array('name'=>'Sửa tin nhắn','link'=>'#','class'=>'active');
        $id=$_GET['id'];
        $data=AMsgUser::getDataById($id);
        $data_attachment=AMsgUser::getAttachmentById($id);
        $this->render("edit",array("data"=>$data,"data_attachment"=>$data_attachment));
    }
    public function actionAjaxEditMsg(){
        $id=isset($_POST['id'])?$_POST['id']:0;
        $title_msg=$_POST['title_msg'];
        $description=$_POST['description'];
        $picture=$_POST['picture'];
        $create_date=time();
        $create_user=Yii::app()->user->name;
        if($id >0){
            $data_old = AMsgUser::getDataById($id);
            $attachment_old=AMsgUser::getAttachmentById($id);
            $data = array(
            "title"=>array("value"=>$title_msg,"type"=>2),
            "content"=>array("value"=>$description,"type"=>2),
            "create_date"=>array("value"=>$create_date,"type"=>1),
            "id"=>array("value"=>$id,"type"=>1),
            );
            $table = "c_msg_user";
            $msg_id= CommonDB::updateRow($table,$data,array("id"));
            if($_POST['picture']!=""){
                $data_attachment=array(
                "filename"=>array("value"=>$_POST['picture'],"type"=>2),
                "create_date"=>array("value"=>$create_date,"type"=>1),
                "create_user"=>array("value"=>$create_user,"type"=>1),
                "msg_id"=>array("value"=>$id,"type"=>1),
                ); 
                $table = "c_msg_attachment";
                if(empty($attachment_old)){
                    $result=CommonDB::insertRow($table,$data_attachment);
                }else{
                    $result= CommonDB::updateRow($table,$data_attachment,array("msg_id")); 
                }

            }
            if($msg_id >0){
                $act = 2;
                $obj = 29;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',title: '.$data_old["title"].',content: '.$data_old["content"].'} thành';
                $content .= ' {id: '.$id.',title: '.$title_msg.',content: '.$description.'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionAjaxDelete(){
        $time_now = time();
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $data_old = AMsgUser::getDataById($id);
        if($id >0){
            $table  = "c_msg_user";
            $result = CommonDB::deleteRow($table,array("id"=>$id));
            $table  = "c_msg_attachment";
            $result1 = CommonDB::deleteRow($table,array("msg_id"=>$id));
            if($result >0){
                $act = 3;
                $obj = 29;
                $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$id.',Người gửi: '.$data_old["user_from"].',Người nhận: '.$data_old["user_to"].',title: '.$data_old["title"].'}';
                $result = AAdminLog::insertAdminLog($act,$obj,$content);
                echo 1;exit();
            }else{
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }
    public function actionCreate(){ 
        $this->breadcrumbs[] = array('name'=>'Thêm tin nhắn','link'=>'#','class'=>'active');
        $this->render("create");
    }
    public function actionAjaxSaveMsg(){
        $all_user=$_POST['all_user'];
        $title_msg=$_POST['title_msg'];
        $description=$_POST['description'];
        $picture=$_POST['picture'];
        $user_from='admin'; 
        $user_from_id=17; 
        if($all_user==1){
            $arr_file=AMsgUserTest::getAllUserId();
            $is_read=json_encode($arr_file);
        }else{
            $user_to=$_POST['user_to'];
            if($user_to==""){
                echo "- Bạn phải nhập tên người nhận";die;
            }
            $arr=User::getDataByUsername($user_to);
            if(empty($arr)){
                echo "- Người nhận không tồn tại";die;
            }else{
                $user_to_id=$arr['id'];
            }
        }
        $from_admin=1;
        $status=1;
        $create_date=time();
        $data = array(
        "user_from"=>array("value"=>$user_from,"type"=>2),
        "user_from_id"=>array("value"=>$user_from_id,"type"=>1),
        "user_to"=>array("value"=>$user_to,"type"=>2),
        "user_to_id"=>array("value"=>$user_to_id,"type"=>1),
        "from_admin"=>array("value"=>$from_admin,"type"=>1),
        "status"=>array("value"=>$status,"type"=>1),
        "is_read"=>array("value"=>$is_read,"type"=>1),
        "title"=>array("value"=>$title_msg,"type"=>2),
        "content"=>array("value"=>$description,"type"=>2),
        "create_date"=>array("value"=>$create_date,"type"=>1),
        "create_user"=>array("value"=>$user_from,"type"=>2),
        );
        $table = "c_msg_user";
        $msg_id= CommonDB::insertRow($table,$data);
        if($_POST['picture']!=""){
            $data_attachment=array(
            "msg_id"=>array("value"=>$msg_id,"type"=>1),
            "filename"=>array("value"=>$_POST['picture'],"type"=>2),
            "create_date"=>array("value"=>$create_date,"type"=>1),
            "create_user"=>array("value"=>$user_from,"type"=>2),
            ); 
            $table = "c_msg_attachment";
            $attachment_id= CommonDB::insertRow($table,$data_attachment);
        }

        if($msg_id >0){
            $act = 1;
            $obj = 29;
            $content = LogConfig::$action[$act].' '.LogConfig::$object[$obj].': {id: '.$msg_id.',Người gửi: '.$user_from.',Người nhận: '.$user_to.',title: '.$title_msg.',status: '.$status.'}';
            $result = AAdminLog::insertAdminLog($act,$obj,$content);
            echo 1;exit();
        }else{
            echo " Thêm mới thất bại";
        }
    }
}
